package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.exception.PlatformBizErrorCodes;
import com.iwomy.secureplat.common.core.util.MsgUtils;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;

import com.iwomy.secureplat.common.security.util.SecurityUtils;
import com.iwomy.secureplat.platform.dto.BasicCommunicationModelDTO;
import com.iwomy.secureplat.platform.entity.BasicCommunicationModelEntity;
import com.iwomy.secureplat.platform.entity.BasicDeviceModelEntity;
import com.iwomy.secureplat.platform.exception.PlatformException;
import com.iwomy.secureplat.platform.service.BasicCommunicationModelService;
import com.iwomy.secureplat.platform.service.BasicDeviceModelService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import cn.hutool.core.util.ObjectUtil;

import java.util.List;

/**
 * 通讯模型表
 *
 * @author pig
 * @date 2023-11-14 11:42:05
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicCommunicationModel")
@Tag(description = "basicCommunicationModel", name = "通讯模型表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicCommunicationModelController {

	private final BasicCommunicationModelService basicCommunicationModelService;

	private final BasicDeviceModelService basicDeviceModelService;


	/**
	 * 分页查询
	 *
	 * @param page                       分页对象
	 * @param basicCommunicationModelDTO 通讯模型表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_basicCommunicationModel_view')")
	public R getBasicCommunicationModelPage(@ParameterObject Page page, @ParameterObject BasicCommunicationModelDTO basicCommunicationModelDTO) {
		return R.ok(basicCommunicationModelService.getBasicCommunicationModelPage(page, basicCommunicationModelDTO));
	}


	/**
	 * 通过id查询通讯模型表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_basicCommunicationModel_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(basicCommunicationModelService.getById(id));
	}

	/**
	 * 新增通讯模型表
	 *
	 * @param basicCommunicationModel 通讯模型表
	 * @return R
	 */
	@Operation(summary = "新增通讯模型表", description = "新增通讯模型表")
	@SysLog("新增通讯模型表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_basicCommunicationModel_add')")
	public R save(@RequestBody BasicCommunicationModelEntity basicCommunicationModel) {
		BasicCommunicationModelEntity wrapperEntity = new BasicCommunicationModelEntity();
		wrapperEntity.setName(basicCommunicationModel.getName());
		LambdaQueryWrapper<BasicCommunicationModelEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicCommunicationModelEntity one = basicCommunicationModelService.getOne(wrapper);
		if (ObjectUtil.isNotNull(one)) {
//			throw new PlatformException("通讯模型已存在");
			throw new PlatformException(MsgUtils.getMessage(PlatformBizErrorCodes.COMMUNICATIONMODEL_ID_EXIST));
		}
		basicCommunicationModel.setDeptId(SecurityUtils.getUser().getDeptId());
		return R.ok(basicCommunicationModelService.save(basicCommunicationModel));
	}

	/**
	 * 修改通讯模型表
	 *
	 * @param basicCommunicationModel 通讯模型表
	 * @return R
	 */
	@Operation(summary = "修改通讯模型表", description = "修改通讯模型表")
	@SysLog("修改通讯模型表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_basicCommunicationModel_edit')")
	public R updateById(@RequestBody BasicCommunicationModelEntity basicCommunicationModel) {
		long count = basicDeviceModelService
				.count(Wrappers.lambdaQuery(new BasicDeviceModelEntity()).eq(BasicDeviceModelEntity::getCommunicationModelId, basicCommunicationModel.getId()));

		if (count > 0){
//			return R.failed("通讯模型已被使用不允许修改");
			return R.failed(MsgUtils.getMessage(PlatformBizErrorCodes.COMMUNICATIONMODEL_ID_USED_MODIFY));
		}

		BasicCommunicationModelEntity wrapperEentity = new BasicCommunicationModelEntity();
		wrapperEentity.setName(basicCommunicationModel.getName());
		LambdaQueryWrapper<BasicCommunicationModelEntity> wrapper = Wrappers.lambdaQuery(wrapperEentity);
		BasicCommunicationModelEntity one = basicCommunicationModelService.getOne(wrapper);
		if (ObjectUtil.isNotNull(one) && !one.getId().equals(basicCommunicationModel.getId())) {
//			throw new PlatformException("通讯模型已存在");
			throw new PlatformException(MsgUtils.getMessage(PlatformBizErrorCodes.COMMUNICATIONMODEL_ID_EXIST));
		}
		return R.ok(basicCommunicationModelService.updateById(basicCommunicationModel));
	}

	/**
	 * 启用禁用
	 * @param basicCommunicationModel 通讯模型
	 * @return R
	 */
	@Operation(summary = "启用禁用通讯模型" , description = "启用禁用通讯模型" )
	@SysLog(expression = "'启用禁用通讯模型'.concat(#basicCommunicationModel.remarks)" )
	@PutMapping("/enableOrDisable")
	@PreAuthorize("@pms.hasPermission('platform_basicCommunicationModel_enableOrDisable')" )
	public R enableOrDisable(@RequestBody BasicCommunicationModelDTO basicCommunicationModel) {

		BasicCommunicationModelEntity basicCommunicationModelEntity = new BasicCommunicationModelEntity();
		basicCommunicationModelEntity.setId(basicCommunicationModel.getId());
		basicCommunicationModelEntity.setRemarks(basicCommunicationModel.getRemarks());
		basicCommunicationModelEntity.setState(basicCommunicationModel.getState());
		return R.ok(basicCommunicationModelService.updateById(basicCommunicationModelEntity));
	}

	/**
	 * 通过id删除通讯模型表
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除通讯模型表", description = "通过id删除通讯模型表")
	@SysLog("通过id删除通讯模型表")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_basicCommunicationModel_del')")
	public R removeById(@RequestBody Long[] ids) {

		long count = basicDeviceModelService.count(Wrappers.<BasicDeviceModelEntity>lambdaQuery().in(BasicDeviceModelEntity::getCommunicationModelId, ids));

		if (count > 0)
//			return R.failed("通讯模型已被使用不允许删除");
		return R.failed(MsgUtils.getMessage(PlatformBizErrorCodes.COMMUNICATIONMODEL_ID_USED_DELETE));

		return R.ok(basicCommunicationModelService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param basicCommunicationModel 查询条件
	 * @param ids                     导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_basicCommunicationModel_export')")
	public List<BasicCommunicationModelEntity> export(BasicCommunicationModelEntity basicCommunicationModel, Long[] ids) {
		return basicCommunicationModelService.list(Wrappers.lambdaQuery(basicCommunicationModel).in(ArrayUtil.isNotEmpty(ids), BasicCommunicationModelEntity::getId, ids));
	}


	/**
	 * 分页查询
	 *
	 * @param basicCommunicationModel 通讯模型
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/list")
	public R getBasicCommunicationModelList(@ParameterObject BasicCommunicationModelEntity basicCommunicationModel) {
		return R.ok(basicCommunicationModelService.pageByScope(basicCommunicationModel));
	}
}